package com.rxmvp.rxjavademo.RxRetrofit;

import com.rxmvp.rxjavademo.RxRetrofit.exception.CodeException;
import com.rxmvp.rxjavademo.ToastUtil;

import java.net.ConnectException;
import java.net.SocketTimeoutException;
import java.net.UnknownHostException;

import retrofit2.adapter.rxjava.HttpException;
import rx.Subscriber;

/**
 * Created by Administrator on 2017/1/13.
 */

public abstract class BaseSubscriber<T> extends Subscriber<T> {
    @Override
    public void onCompleted() {
    }

    @Override
    public void onError(Throwable e) {
        //处理各种异常
        if (e instanceof CodeException) {
            CodeException exception = (CodeException) e;
            // ToastUtil.showToast(exception.message);
        } else if (e instanceof UnknownHostException) {
             ToastUtil.showToast("请打开网络");
        } else if (e instanceof SocketTimeoutException) {
             ToastUtil.showToast( "请求超时");
        } else if (e instanceof ConnectException) {
             ToastUtil.showToast("连接失败");
        } else if (e instanceof HttpException) {
             ToastUtil.showToast("请求超时");
        }else {
              ToastUtil.showToast("请求失败");
        }
        e.printStackTrace();
        onFailure(); //回调方法
    }

    @Override
    public void onNext(T t) {
    }

    @Override
    public void onStart() {
        super.onStart();
    }

    public abstract void onFailure();

}
